<?php
namespace Backend\Controllers\Member;

use Backend\Controllers\BaseController;
use Backend\Libraries\Helper;
use Backend\Models\Member\MemberList;

class ListController extends BaseController
{
    public function initialize()
    {
        parent::initialize();
    }

    public function getsAction()
    {
        $data = [
            'page' => $this->request->getPost('page'),
            'size' => $this->request->getPost('pageSize')
        ];
        if ($data['page'] == '' || !is_numeric($data['page'])) {
            return $this->json(['code' => 500, 'msg' => '未获取到当前的页数！']);
        }
        if ($data['size'] == '' || !is_numeric($data['size'])) {
            return $this->json(['code' => 500, 'msg' => '未获取到当前的每页的条数！']);
        }
        $data['size'] = intval($data['size']);
        $data['page'] = intval($data['page']);
        $offset       = intval(($data['page'] - 1) * $data['size']);
        $model        = new MemberList();
        $list         = $model->gets($data['size'], $offset);
        if ($list !== false) {
            return $this->json([
                'code' => 200,
                'msg'  => '获取成功!',
                'list' => $list
            ]);
        }
        return $this->json([
            'code' => 500,
            'msg'  => '获取失败，系统错误~'
        ]);
    }

    public function getCountAction()
    {
        $cnt = (new MemberList())->getCount();
        if ($cnt === false) {
            return $this->json([
                'code' => 500,
                'msg'  => '获取失败，系统错误~'
            ]);
        }
        return $this->json([
            'code'  => 200,
            'count' => $cnt
        ]);
    }

    public function addAction()
    {
        $data = [
            'phone'     => $this->request->getPost('phone'),
            'name'      => $this->request->getPost('name'),
            'sex'       => $this->request->getPost('sex'),
            'work_id'   => $this->request->getPost('work_id'),
            'status'    => $this->request->getPost('status'),
            'age'       => $this->request->getPost('age'),
            'role_id'   => $this->request->getPost('role_id'),
            'sid'       => $this->request->getPost('sid'),
            'work_time' => $this->request->getPost('work_time'),
            'skill'     => $this->request->getPost('skill')
        ];
        if ($data['phone'] == '' || !Helper::isMobile($data['phone'])) {
            return $this->json([
                'code' => 501,
                'msg'  => '手机号码不能为空'
            ]);
        }
        $model = new MemberList();
        $user  = $model->getByAccount($data['phone']);
        if ($user != false) {
            return $this->json(['code' => 501, 'msg' => '手机号码已经存在']);
        }
        if ($data['name'] == '') {
            return $this->json(['code' => 501, 'msg' => '员工名称不能为空']);
        }
        if ($data['work_id'] == '' || !is_numeric($data['work_id'])) {
            return $this->json(['code' => 501, 'msg' => '请选择员工类型']);
        }
        if ($data['role_id'] == '' || !is_numeric($data['role_id'])) {
            return $this->json(['code' => 501, 'msg' => '请选择员工角色！']);
        }
        if ($data['sid'] == '') {
            return $this->json(['code' => 501, 'msg' => '请选择商户']);
        }
        $data['account']   = $data['phone'];
        $data['status']    = 1;
        $data['role_id']   = intval($data['role_id']);
        $data['work_id']   = intval($data['work_id']);
        $data['create_at'] = date('Y-m-d H:i:s');
        $data['password']  = md5(substr($data['account'], -6));
        if ($model->add($data)) {
            return $this->json(['code' => 200, 'msg' => '添加成功']);
        }
        return $this->json(['code' => 501, 'msg' => '系统错误']);
    }

    //    public function editAction() {
    //        $data = [
    //            'phone'     => $this->request->getPost('phone'),
    //            'name'      => $this->request->getPost('name'),
    //            'sex'       => $this->request->getPost('sex'),
    //            'work_id'   => $this->request->getPost('work_id'),
    //            'status'    => $this->request->getPost('status'),
    //            'age'       => $this->request->getPost('age'),
    //            'role_id'   => $this->request->getPost('role_id'),
    //            'sid'       => $this->request->getPost('sid'),
    //            'work_time' => $this->request->getPost('work_time'),
    //            'skill'     => $this->request->getPost('skill')
    //        ];
    //        if($data['phone'] == '' || !Helper::isMobile($data['phone'])) {
    //            return $this->json([
    //                'code' => 501,
    //                'msg'  => '手机号码不能为空'
    //            ]);
    //        }
    //        $model = new MemberList();
    //        if($data['name'] == '') {
    //            return $this->json(['code' => 501, 'msg' => '员工名称不能为空']);
    //        }
    //        if($data['work_id'] == '' || !is_numeric($data['work_id'])) {
    //            return $this->json(['code' => 501, 'msg' => '请选择员工类型']);
    //        }
    //        if($data['role_id'] == '' || !is_numeric($data['role_id'])) {
    //            return $this->json(['code' => 501, 'msg' => '请选择员工角色！']);
    //        }
    //        if($data['sid'] == '') {
    //            return $this->json(['code' => 501, 'msg' => '请选择商户']);
    //        }
    //        $data['role_id']   = intval($data['role_id']);
    //        $data['work_id']   = intval($data['work_id']);
    //        $data['last_update_at'] = date('Y-m-d H:i:s');
    //        if($model->edit($data)) {
    //            return $this->json(['code' => 200, 'msg' => '添加成功']);
    //        }
    //        return $this->json(['code' => 501, 'msg' => '系统错误']);
    //    }

    public function getAction()
    {
        $id = $this->request->getPost('wid');
        if ($id == '' || !is_numeric($id)) {
            return $this->json([
                'code' => 501,
                'msg'  => '参数丢失～'
            ]);
        }
        $model = new Role();
        $ret   = $model->get($id);
        if ($ret !== false) {
            return $this->json([
                'code' => 200,
                'ret'  => $ret
            ]);
        }
        return $this->json([
            'code' => 501,
            'msg'  => '系统错误，请稍候再试～'
        ]);
    }

    public function getWorkTypeOptsAction()
    {
        $model = new MemberList();
        $ret   = $model->getWorkName();
        if ($ret !== false) {
            return $this->json(['code' => 200, 'ret' => $ret]);
        }
        return $this->json(['code' => 501, ',msg' => '系统错误，请稍候再试~']);
    }

    public function getRoleOptsAction()
    {
        $model = new MemberList();
        $ret   = $model->getRoleName();
        if ($ret !== false) {
            return $this->json(['code' => 200, 'ret' => $ret]);
        }
        return $this->json(['code' => 501, ',msg' => '系统错误，请稍候再试~']);
    }

    public function getStoreOptsAction()
    {
        $model = new MemberList();
        $ret   = $model->getStoreName();
        if ($ret !== false) {
            return $this->json(['code' => 200, 'ret' => $ret]);
        }
        return $this->json(['code' => 501, ',msg' => '系统错误，请稍候再试~']);
    }
}
